package com.example.testandroid;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NumGuess extends Activity {

	private int tar;
	
	TextView tv;
	EditText et;
	Button btn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_num_guess);
		tv = (TextView)findViewById(R.id.show_text);
		et = (EditText)findViewById(R.id.et);
		btn = (Button)findViewById(R.id.btn);
		newGame();
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(btn.getText().toString().equals("ok")){
					int tmp = Integer.parseInt(et.getText().toString());
					if(tmp == tar){
						tv.setText("恭喜猜对了！");
						btn.setText("再来一次");
					}else if(tmp < tar){
						tv.setText("猜小了，请重试:");
						et.setText("");
					}else{
						tv.setText("猜大了，请重试:");
						et.setText("");
					}
				}else newGame();
			}
		});
		
		
		
		Button swithBtn = (Button)findViewById(R.id.switch_btn);
		
		swithBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent it = new Intent();
				it.setAction(Intent.ACTION_MAIN);
				it.addCategory(Intent.CATEGORY_HOME);
				startActivity(it);
			}
		});
	}
	
	private void newGame(){
		tv.setText("请输入0-9之间的数字:");
		et.setText("");
		btn.setText("ok");
		tar = (int)(Math.random()*9);
	}
}
